<?php
/**
 * User: 浪子
 * Date: 2018-04-26
 * Time: 12:40
 */


$headers = array();
if (!function_exists('getallheaders')) {
    foreach ($_SERVER as $name => $value) {
        if (substr($name, 0, 5) == 'HTTP_') {
            $headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
        }
    }
}else{
    $headers = getallheaders();
}

$Authorization = $headers["Authorization"];

$login = [
    "err"=>0,
    "body"=>[
        "uid"=>13,
        "name"=>"Hola",
        "token"=>"oneToken"
    ]
];
$pwd = ["err"=>0];

$page = isset($_REQUEST["page"])?$_REQUEST["page"]:1;
$count= isset($_REQUEST["count"])?$_REQUEST["count"]:20;
$user_list = [
    "err"=>0,
    "body"=>[
        "total"=>7,
        "list"=>[
            ["id"=>($page-1)*$count+1,"account"=>"admin","name"=>"n-admin","status"=>1],
            ["id"=>($page-1)*$count+2,"account"=>"view","name"=>"n-view","status"=>1],
            ["id"=>($page-1)*$count+3,"account"=>"test","name"=>"n-test","status"=>0],
        ]
    ]
];
$user_status = [
    "err"=>0
];
$user_dele = [
    "err"=>0
];
$user_edit = [
    "err"=>0,
    "body"=>[
        "id"=>7
    ]
];
$user_detail = [
    "err"=>0,
    "body"=>[
        "id"=>7,
        "account"=>"hola",
        "name"=>"管理员"
    ]
];
$product_list = [
    "err"=>0,
    "body"=>[
        "total"=>7,
        "list"=>[
            ["id"=>($page-1)*$count+1,"code"=>1,"name"=>"千锤百炼一口锅","video_length"=>"1800","price"=>"9900","sales"=>"0","status"=>1],
            ["id"=>($page-1)*$count+2,"code"=>2,"name"=>"华为P100","video_length"=>"3600","price"=>"800000","sales"=>"799900","status"=>0],
            ["id"=>($page-1)*$count+3,"code"=>3,"name"=>"有机五常大米20KG","video_length"=>"3500","price"=>"8000","sales"=>"5900","status"=>1],
            ["id"=>($page-1)*$count+4,"code"=>4,"name"=>"有机五常大米20KG","video_length"=>"3500","price"=>"8000","sales"=>"5900","status"=>1],
            ["id"=>($page-1)*$count+5,"code"=>5,"name"=>"有机五常大米20KG","video_length"=>"3500","price"=>"8000","sales"=>"5900","status"=>1],
            ["id"=>($page-1)*$count+6,"code"=>6,"name"=>"有机五常大米20KG","video_length"=>"3500","price"=>"8000","sales"=>"5900","status"=>1],
            ["id"=>($page-1)*$count+7,"code"=>7,"name"=>"有机五常大米20KG","video_length"=>"3500","price"=>"8000","sales"=>"5900","status"=>1],
            ["id"=>($page-1)*$count+8,"code"=>8,"name"=>"有机五常大米20KG","video_length"=>"3500","price"=>"8000","sales"=>"5900","status"=>1],
            ["id"=>($page-1)*$count+9,"code"=>9,"name"=>"有机五常大米20KG","video_length"=>"3500","price"=>"8000","sales"=>"5900","status"=>1],
            ["id"=>($page-1)*$count+10,"code"=>10,"name"=>"有机五常大米20KG","video_length"=>"3500","price"=>"8000","sales"=>"5900","status"=>1],
            ["id"=>($page-1)*$count+11,"code"=>11,"name"=>"有机五常大米20KG","video_length"=>"3500","price"=>"8000","sales"=>"5900","status"=>1],
            ["id"=>($page-1)*$count+12,"code"=>12,"name"=>"有机五常大米20KG","video_length"=>"3500","price"=>"8000","sales"=>"5900","status"=>1],
            ["id"=>($page-1)*$count+13,"code"=>13,"name"=>"有机五常大米20KG","video_length"=>"3500","price"=>"8000","sales"=>"5900","status"=>1],
            ["id"=>($page-1)*$count+14,"code"=>14,"name"=>"有机五常大米20KG","video_length"=>"3500","price"=>"8000","sales"=>"5900","status"=>1],
            ["id"=>($page-1)*$count+15,"code"=>15,"name"=>"有机五常大米20KG","video_length"=>"3500","price"=>"8000","sales"=>"5900","status"=>1],
            ["id"=>($page-1)*$count+16,"code"=>16,"name"=>"有机五常大米20KG","video_length"=>"3500","price"=>"8000","sales"=>"5900","status"=>1],
            ["id"=>($page-1)*$count+17,"code"=>17,"name"=>"有机五常大米20KG","video_length"=>"3500","price"=>"8000","sales"=>"5900","status"=>1],
            ["id"=>($page-1)*$count+18,"code"=>18,"name"=>"有机五常大米20KG","video_length"=>"3500","price"=>"8000","sales"=>"5900","status"=>1],
        ]
    ]
];
$product_status = [
    "err"=>0
];
$product_dele = [
    "err"=>0
];
$product_detail = [
    "err"=>0,
    "body"=>[
        "id"=>3,
        "name"=>"有机五常大米20KG",
        "search_name"=>"yjwcdm20kg",
        "video"=>"http://yj.mp4",
        "video_length"=>"3500",
        "price"=>"8000",
        "sales"=>"5900",
        "description"=>"有机五常大米20KG，来自东北，纯天然，无污染",
    ]
];
$product_edit = [
    "err"=>0,
    "body"=>[
        "id"=>7
    ]
];
$product_promotion = [
    "err"=>0
];
$product_search = [
  "message"=>1,"code"=>200,"redirect"=>"", "value"=>[
        ["id"=>1,"name"=>"中华好香烟","time"=>"3000"],
        ["id"=>2,"name"=>"华为PX","time"=>"3800"],
        ["id"=>3,"name"=>"可可可乐","time"=>"180"],
        ["id"=>4,"name"=>"老板躺椅","time"=>"2702"],
        ["id"=>5,"name"=>"VR全世界","time"=>"3134"],
        ["id"=>6,"name"=>"良心小馒头","time"=>"5432"]
    ]
];

$file_upload = [
    "err"=>-1,
    "url"=>""
];
if($_GET["tag"] == "file_upload"){
//    $file_upload["file"] = json_encode($_FILES["pic"]);
    if ((($_FILES["pic"]["type"] == "image/gif") || ($_FILES["pic"]["type"] == "image/jpeg") || ($_FILES["pic"]["type"] == "image/pjpeg"))) {
        $file_name = date("YmdHis").".jpg";
        if ($_FILES["pic"]["error"] > 0)
        {
            $file_upload["url"] = "Return Code: " . $_FILES["pic"]["error"] . "<br />";
        } else {
//            echo "Upload: " . $_FILES["file"]["name"] . "<br />";
//            echo "Type: " . $_FILES["file"]["type"] . "<br />";
//            echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
//            echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

            if (file_exists("upload/" . $file_name))
            {
                $file_upload["url"] = $_FILES["pic"]["name"] . " already exists. ";
            } else {
                move_uploaded_file($_FILES["pic"]["tmp_name"], "upload/" . $file_name);
//                echo "Stored in: " . "upload/" . $file_name;
                $file_upload["err"] = 0;
                $file_upload["url"] = "upload/" . $file_name;
            }
        }
    } else {
        $file_upload["url"] = "Invalid file";
    }
    echo "<script language=\"JavaScript\">";
    echo "window.parent.postMessage('".json_encode($file_upload)."','*');";
    echo "</script>";
}else{
    header("content-type:application/json;charset=utf8");
    header("Authorization: ".$Authorization);
    $s = "return $".$_GET["tag"].";";
    exit(json_encode(eval($s)));
}
